import { Footer } from '@/components';
import { login } from '@/services/ant-design-pro/api';
import {
  LockOutlined,
  UserOutlined,
} from '@ant-design/icons';
// @ts-ignore
import { LoginForm, ProFormText } from '@ant-design/pro-components';
import { Helmet, Link, history } from '@umijs/max';
// @ts-ignore
import {Tabs, message } from 'antd';
import { createStyles } from 'antd-style';
// @ts-ignore
import Settings from '../../../../config/defaultSettings';
import './Login.less'
// @ts-ignore
import {React} from 'react';

const useStyles = createStyles(() => {
  return {
    action: {
      marginLeft: '8px',
      color: '#52C41A',
      fontSize: '24px',
      verticalAlign: 'middle',
      cursor: 'pointer',
      transition: 'color 0.3s',
      '&:hover': {
        color: "#C3D08B",
      },
    },
    container: {
      display: 'flex',
      flexDirection: 'column',
      height: '100vh',
      overflow: 'auto',
      backgroundImage:
        "url('https://mdn.alipayobjects.com/yuyan_qk0oxh/afts/img/V-_oS6r-i7wAAAAAAAAAAAAAFl94AQBr')",
      backgroundSize: '100% 100%',
    },
  };
});

const Login: React.FC = () => {
  const { styles } = useStyles();
  const handleSubmit = async (values: API.LoginParams) => {
    try {
      // 登录
      const res = await login({
        ...values,
      });
      if (res.code && res.data && res.code === '200') {
        // console.log(res)
        console.log(String(res.data.id))
        localStorage.setItem("userId", String(res.data.id))
        message.success('登录成功！');
        history.push('/welcome');
      }
      else{
        message.error("失败");
        console.log(res)
      }
      // 如果失败去设置用户错误信息
    } catch (error) {
      const defaultLoginFailureMessage = '登录失败，请重试！';
      console.log(error);
      message.error(defaultLoginFailureMessage);
    }
  };

  return (
    <div className={styles.container}>
      <Helmet>
        <title>
          {'登录'}- {Settings.title}
        </title>
      </Helmet>
      <div
        style={{
          flex: '1',
          padding: '32px 0',
        }}
      >
        <LoginForm
          contentStyle={{
            minWidth: 280,
            maxWidth: '75vw',
          }}
          logo={
            <img
              alt="logo"
              src="https://xiaoxiaobaiyang.oss-cn-shenzhen.aliyuncs.com/mylibrary/web-image/logo1.png"
            />
          }
          title="MyLibrary"
          subTitle={'MyLibrary 愿此行，终抵群星'}
          onFinish={async (values) => {
            await handleSubmit(values as API.LoginParams);
          }}
        >
          <Tabs centered>
            <Tabs.TabPane key={'account'} tab={'账号密码登录'} />
          </Tabs>

          {
            <>
              <ProFormText
                name="username"
                fieldProps={{
                  size: 'large',
                  prefix: <UserOutlined />,
                }}
                placeholder={'用户名: zjh'}
                rules={[
                  {
                    required: true,
                    message: '用户名是必填项！',
                  },
                ]}
              />
              <ProFormText.Password
                name="password"
                fieldProps={{
                  size: 'large',
                  prefix: <LockOutlined />,
                }}
                placeholder={'密码: 123'}
                rules={[
                  {
                    required: true,
                    message: '密码是必填项！',
                  },
                ]}
              />
            </>
          }
          <div
            style={{
              marginBottom: 24,
            }}
          >
            <Link to={'/user/register'}>新用户注册</Link>
            <a
              style={{
                float: 'right',
                color: "#52C41A",
              }}
            >
              忘记密码 ?
            </a>
          </div>
        </LoginForm>
      </div>
      <Footer />
    </div>
  );
};
export default Login;
